package com.nwpu.coursesupport.tool;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
	private static SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static Date getNextDate(Date date,int days){
		Date nextDate=new Date(date.getTime() + (long)days * 24 * 60 * 60 * 1000);  
		return nextDate;
	}
	
	public static Date getBeginOfDate(Date date){
		SimpleDateFormat rsf=new SimpleDateFormat("yyyy-MM-dd");// HH:mm
		String s=rsf.format(date);
		try {
			date=sf.parse(s+" 00:00:00");
		} catch (ParseException e) {
		}
		return date;
	}
	
	public static Date getEndOfDate(Date date){
		SimpleDateFormat rsf=new SimpleDateFormat("yyyy-MM-dd");// HH:mm
		String s=rsf.format(date);
		try {
			date=sf.parse(s+" 23:59:59");
		} catch (ParseException e) {
		}
		return date;
	}
	
	public static Date parseDate(String date){
		if(StringUtil.isEmpty(date)){
			return null;
		}
		Date d=null;
		try {
			d=sf.parse(date);
		} catch (ParseException e) {
		}
		return d;
	}
	
	public static int getDuration(Date date1,Date date2){
		int minutes=0;
		try {
			String fromDate = sf.format(date1);  
			String toDate = sf.format(date2);  
			long from=sf.parse(fromDate).getTime();
			long to = sf.parse(toDate).getTime();  
			minutes = (int) ((to - from)/(1000 * 60)); 
		} catch (ParseException e) {
		}  
		return minutes;
	}
	
	//public Date 
	public static void main(String[] args) throws ParseException {
		Date date=new Date();
		String now=sf.format(date);
		System.out.println(now);
		sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		date=sf.parse(now+" 23:59:59");
		System.out.println(sf.format(date));
	}

}
